import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store= new Vuex.Store({
  state: {
    isLogin:false,
    Loading: false,
    username: localStorage.getItem('username') ? localStorage.getItem('username') : '',
    sid: localStorage.getItem('sid') ? localStorage.getItem('sid') : '',
    gis4_phone:'',
    gis4_imei:'',//测试用
    gis4_appVersion:0,
    gis4_phoneTime:'',
    userToken:localStorage.getItem('userToken') ? localStorage.getItem('userToken') : '',
    latitude:'',
    longitude:'',
    speed:localStorage.getItem('speed') ? localStorage.getItem('speed') : '',

    workModeId:localStorage.getItem('workModeId') ? localStorage.getItem('workModeId') : '',
    workModeName:localStorage.getItem('workModeName') ? localStorage.getItem('workModeName') : '',
    host:localStorage.getItem('apiHost'),
    lineWorkTagKey:'1',
    planId:'',
    floodsWarning:null,
    floodsMessage:{},
    countWarningNumber:'',
    workTime:'',//计算和显示进入软件的时间
    test:'',//测试用的，如动态显示百度sdk回调的结果
    compass:null,//测试指南针用作观察是否移动的
    fileId:0,//文件自增唯一id
    //文件详情
    fileList:[
        {
          detail:{
            basicInfo:{fileId:0,fileName:"文件名称",filePath:"文件路径",mime类型:"",createdTime:"创建日期"},
            uploadInfo:{isOK:false,progress:0,beginTime:"0000"},
            aboutInfo:{userId:"",type:"资料上报"}
          }
        }
    ],
    userInfo:[],//用户状态管理 {sid:'1001',userName:'初始用户',passWord:''}
    planNo:"",
      //对讲状态管理 注释：是否已登录对讲服务器，memberManage，channelManage是获取到的成员管理数据和频道管理数据
    intercomList : {
      isLoginServe : false,
      memberManage : {
        currMember : { //当前用户信息
          "channel":{
            "creatorId":1001000,
            "id":1001,
            "isTemporary":false,
            "memberCount":3,
            "name":"group1",
            "searchable":true,
            "userCount":2,
            "pwd":"",
          },
          "iId":1001002,
          "isCurrent":true,
          "isLocalTalking":false,
          "isTalking":false,
          "name":"user2",
          "nick":"用户2",
        },
        contacts : {
          "1001003": {
            "audioSource": 0,
            "iId": 1001003,
            "nick": "用户3",
            "online": false,
            "pending": false,
            "selected": false
          },
        },
        pendingContacts : { },
        pendingMembers : { },
        talkingUser:{
          "audioSource":0,
          "iId":1001005,
          "isCurrent":false,
          "isLocalTalking":true,
          "isTalking":false,
          "nick":"某个用户",
          "session":10,
          "channel":{
            "allowOrderBg":false,
            "creatorId":0,
            "creatorNick":"",
            "expanded":-1,
            "id":1001,
            "isTemporary":false,
            "memberCount":6,
            "monitors":"",
            "mutes":"",
            "name":"鹰潭工务段测试组",
            "needApply":false,
            "priors":"",
            "pwd":"",
            "searchable":true,
            "userCount":3
          }
        },
      },
      channelManage : {
        currChannel : { //当前频道信息
          "creatorId":1001001,
          "creatorNick":"系统",
          "id":1001,
          "isTemporary":false,
          "memberCount":1,
          "name":"未联网频道",
          "searchable":true,
          "userCount":0,
          "pwd":"",
        },
        allChannel :[
          {
            "creatorId":1001001,
            "creatorNick":"系统",
            "id":1001,
            "isTemporary":false,
            "memberCount":1,
            "name":"未联网频道2",
            "searchable":true,
            "userCount":0,
            "pwd":"",
          },
          {
            "creatorId":1001002,
            "creatorNick":"沈万三",
            "id":1002,
            "isTemporary":false,
            "memberCount":2,
            "name":"未联网频道2",
            "searchable":true,
            "userCount":0,
            "pwd":"",
          },

            ],
        channelOnlineMember :{//各个频道信息和其在线人员信息
          "0":[],
          "1001":[
            {
              "channel":{
                "creatorId":1001000,
                "creatorNick":"",
                "id":1001,
                "isTemporary":false,
                "memberCount":3,
                "name":"group1",
                "searchable":true,
                "userCount":2
              },
              "iId":1001001,
              "isCurrent":false,
              "isLocalTalking":false,
              "isTalking":false,
              "name":"",
              "nick":"user1"
            },
            {
              "channel":{},
              "iId":1001002,
              "isCurrent":true,
              "isLocalTalking":false,
              "isTalking":false,
              "name":"user2",
              "nick":"user2",
            }
          ],
        },
        channelAllMember : [{"id":"1001001","name":"user1","status":false},{"id":"1001002","name":"user2","status":false},{"id":"1001003","name":"user3","status":false}],//查看指定频道所有人员的id和name
        countChannelNumber :3,//该用户加入的频道数量
      },
    },
      //搜索频道结果
    channelSearch:'',//{"chanId":"1003","chanName":"福州工务段测试","joined":false,"needPwd":false,"onlineCount":1,"memberCount":4}
    apiHost:'',
    intercomHost:'',
    deviceReady:false,//cordova框架是否加载完成
      //需要检测是否安装，配置，运行，打开的apk
    apkConfig:{
      location:{
        isInstall:false,
        config:{},
        isRunning:{},
        needOpen:false
      },
      intercom:{
        isInstall:false,
        config:{},
        isRunning:{},
        needOpen:false
      },
      conference:{
        isInstall:false,
        config:{},
        isRunning:{},
        needOpen:false
      },

    }
  },
  mutations: {
    updateDeviceReady(state, deviceReady){
      state.deviceReady = deviceReady;
      localStorage.setItem('deviceReady', deviceReady);
    },
    updateApiHost(state, apiHost){
      state.apiHost = apiHost;
      localStorage.setItem('apiHost', apiHost);
    },
    updateIntercomHost(state, intercomHost){
      state.intercomHost = intercomHost;
      localStorage.setItem('intercomHost', intercomHost);
    },
    updateApkConfig(state, apkConfig){
      state.apkConfig = apkConfig;
    },
    showLoading(state){
      state.Loading = true
    },

    hideLoading (state) {
      state.Loading = false
    },
    // 修改token，并将token存入localStorage,测试废弃
    changeLogin (state, user) {
      state.userToken = user.userToken;
      localStorage.setItem('userToken', user.userToken);
    },
    //使用方式 method添加...mapMutations(['changeWorkModeId','xx']), 方法内使用this.changeWorkModeId(workModeId);
    changeWorkModeId (state, workModeId) {
      state.workModeId =workModeId;
      localStorage.setItem('workModeId',workModeId);

    },
    changeWorkModeName (state,workModeName) {
      state.workModeName =workModeName;
      localStorage.setItem('workModeName',workModeName);
    },
    updateToken (state, userToken) {
      state.userToken = userToken;
      localStorage.setItem('userToken', userToken);
    },
    updateImei (state, gis4_imei) {
      state.gis4_imei = gis4_imei;
      localStorage.setItem('gis4_imei', gis4_imei);
    },
    updatePhone (state, gis4_phone) {
      state.gis4_phone = gis4_phone;
    },
    updatePhoneTime (state, gis4_phoneTime) {
      state.gis4_phoneTime = gis4_phoneTime;
    },
    updateAppVersion (state, gis4_appVersion) {
      state.gis4_appVersion = gis4_appVersion;
      localStorage.setItem('gis4_appVersion', gis4_appVersion);
    },
    updateLatitude (state, latitude) {
      state.latitude = latitude;
      localStorage.setItem('latitude', latitude);
    },
    updateLongitude(state, longitude) {
      state.longitude = longitude;
      localStorage.setItem('longitude', longitude);
    },
    updateSpeed (state, speed) {
      state.speed = speed;
      localStorage.setItem('speed', speed);
    },

    updateUsername (state, username) {
      state.username = username;
      localStorage.setItem('username',username);
      /*state.username = user.username;
      localStorage.setItem('username',user.username);*/
    },
    updateSid (state, sid) {
      state.sid = sid;
      localStorage.setItem('sid',sid);
      /*state.username = user.username;
      localStorage.setItem('username',user.username);*/
    },
    updateUserInfo (state, userInfo) {
      state.userInfo = userInfo;
      localStorage.setItem('userInfo',JSON.stringify(userInfo));
    },
    updateHost (state, apiHost) {
      state.apiHost = apiHost;
      localStorage.setItem('apiHost',apiHost);
    },
    updateLineWorkTagKey (state, lineWorkTagKey) {
      state.lineWorkTagKey = lineWorkTagKey;
    },
    updatePlanId (state, planId) {
      state.planId = planId;
    },
    updateFloodsMessage (state, floodsMessage) {
      state.floodsMessage = null;//vue监听不到数组的改变 所以清空重置一下就好咯
      state.floodsMessage = floodsMessage;
    },
    updateFloodsWarning (state, floodsWarning) {
      state.floodsWarning = null;
      state.floodsWarning = floodsWarning;

    },
    countWarningNumber(state,countWarningNumber){
      state.countWarningNumber = countWarningNumber;
    },
    updateWorkTime (state, workTime) {
      state.workTime = workTime;
      },
    updateTest (state, Test) {
      state.test = Test;
    },
    updateCompass (state, compass) {
      state.compass = compass;
    },
    addFileList (state,fileList){
        state.fileList.push(fileList);
    },
    updateIntercomList (state,intercomList){
      state.intercomList = intercomList;
    },
    addFileId(state){
      state.fileId++;
    },
    updatePlanNo(state,planNo){
      state.planNo = planNo;
      localStorage.setItem('planNo',planNo);
    },
    updateChannelSearch(state,channelSearch){
      state.channelSearch = channelSearch;
    },
    updateIsLoginServe(state,isLoginServe){
      //isLoginServe boolean
      state.intercomList.isLoginServe = isLoginServe;
    }

  },
  actions: {
    updateUserInfo (context,playLoad) {
      //context.commit 提交一个 mutation，
      // 或者通过 context.state
      // 和 context.getters
      context.commit('updateUserInfo',playLoad);
    },
    //实践中，我们会经常用到 ES2015 的 参数解构 来简化代码（特别是我们需要调用 commit 很多次的时候）：
    increment ({ commit }) {
      commit('increment')
    },
    updateIntercomList(context,playLoad){
      context.commit('updateIntercomList',playLoad);
    },

  },
  getters: {
    // 使用对象展开运算符将 getter 混入 computed 钩子对象中
    //import { mapGetters } from 'vuex'
    // export default {
    //   computed: {
    //     ...mapGetters([
    //       'doneTodosCount',
    //       'anotherGetter',
    //       // ...
    //     ])
    //   }
    // }
    /*//传参形式计算，如store.getters.getTodoById(2)
    getTodoById: (state) => (id) => {
      return state.todos.find(todo => todo.id === id);
    }*/
    isLogin: state => {
      return state.isLogin;
    },
    fileId:state =>{
      return state.fileId;
    },
    /*apiHost:state => {
      return state.apiHost
    },
    apiIntercom:state => {
      return state.apiIntercom
    }*/

  }


});
export default store;